﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Business.Sales;
using ESOL.Business.Sec;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageCustomerOverview : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstCustomerEntity mSelectedCustomer;
        #endregion

        #region Constructor
        public frmManageCustomerOverview()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            List<MstIndustryTypeEntity> listType = MstIndustryTypeBs.GetAllIndustryTypeList();
            MstIndustryTypeEntity industryType = new MstIndustryTypeEntity();
            listType.Insert(0, industryType);
            cmbIndustryType.DataSource = listType;
            cmbIndustryType.DisplayMember = "IndustryTypeName";
            cmbIndustryType.ValueMember = "IndustryTypeCode";
            List<MstPersonEntity> listPerson = new List<MstPersonEntity>();
            listPerson = MstPersonBs.GetPersonForSalesman();
            listPerson.Insert(0, new MstPersonEntity());
            cmbPerson.DataSource = listPerson;
            cmbPerson.DisplayMember = "NameCn";
            cmbPerson.ValueMember = "PersonId";
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
        }

        private void BindCustomerByPerson(string personID)
        {
            List<MstCustomerEntity> list = MstPersonCustomerBs.GetCustomerByPersonID(personID);
            MstCustomerEntity entity = new MstCustomerEntity();
            list.Insert(0, entity);
        }

        private string GetCondition()
        {
            string condition = "";
            //获得用户对应的角色的所有子角色
            SecUserEntity user = ESOL.Business.Sec.SecUserBs.GetUserByID(Common.CommonParameter.CurrentUser.UserId);
            List<SecUserRoleEntity> userroles = SecUserRoleBs.GetSecUserRolesByUserID(user.UserId);
            List<SecRoleEntity> roles = new List<SecRoleEntity>();
            foreach (SecUserRoleEntity userrole in userroles)
            {
                roles.AddRange(SecRoleBs.GetChildrenRolesForID(userrole.RoleId, null));
            }
            //根据子角色获取所有对应的用户
            List<MstPersonEntity> person = new List<MstPersonEntity>();
            if (roles.Count != 0)
            {
                person = MstPersonBs.GetPersonForRoles(roles);
            }
            person.Add(Common.CommonParameter.CurrentUser.PersonEntity);
            //根据用户获取其客户
            List<MstCustomerEntity> customers = MstCustomerBs.GetCustomersForSalesmans(person);
            if (customers.Count == 0)
            {
                condition += " AND MC.CUSTOMER_ID is NULL ";
            }
            else
            {
                condition += " AND MC.CUSTOMER_ID IN (";
                foreach (MstCustomerEntity customer in customers)
                {
                    condition += "'" + customer.CustomerId + "',";
                }
                condition = condition.Substring(0, condition.Length - 1);
                condition += ")";
            }
            if (string.IsNullOrEmpty(cmbPerson.SelectedValue.ToString()) == false)
            {
                MstPersonEntity person_c = MstPersonBs.GetMstPersonByID(cmbPerson.SelectedValue.ToString());
                //根据用户获取其客户
                List<MstCustomerEntity> customers_c = MstCustomerBs.GetCustomersForSalesmans(new List<MstPersonEntity> { person_c });
                if (customers_c.Count == 0)
                {
                    condition += " AND MC.CUSTOMER_ID is NULL ";
                }
                else
                {
                    condition += " AND MC.CUSTOMER_ID IN (";
                    foreach (MstCustomerEntity customer in customers_c)
                    {
                        condition += "'" + customer.CustomerId + "',";
                    }
                    condition = condition.Substring(0, condition.Length - 1);
                    condition += ")";
                }
            }
            if (string.IsNullOrEmpty(cmbIndustryType.Text) == false && cmbIndustryType.SelectedValue != null)
            {
                condition += " AND MC.INDUSTRY_TYPE_CODE = '" + cmbIndustryType.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(txtCustomerName.Text) == false)
            {
                condition += " AND MC.CUSTOMER_NAME LIKE '%" + txtCustomerName.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND MC.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            else
            {
                condition += " AND MC.STATUS != 'STATUS05'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string personId = "";
            if (string.IsNullOrEmpty(cmbPerson.Text) == false && cmbPerson.SelectedValue != null)
            {
                personId = cmbPerson.SelectedValue.ToString();
            }
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "MC.CUSTOMER_NAME";
            string startDate = "";
            string endDate = "";
            if (dtpBeginDate.EditValue != null)
            {
                startDate = ((DateTime)dtpBeginDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00";
            }
            if (dtpEndDate.EditValue != null)
            {
                endDate = ((DateTime)dtpEndDate.EditValue).ToString("yyyy-MM-dd") + " 23:59:59";
            }
            DataTable dt = SalesOrderBs.GetSalesOrderOverview(condition, startDate, endDate, personId, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        private void cmbPerson_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (cmbPerson.SelectedValue != null)
            //{
            //    BindCustomerByPerson(cmbPerson.SelectedValue.ToString());
            //}
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            frmManageBusinessList frm = new frmManageBusinessList(mSelectedCustomer);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        protected override void Click_Delete()
        {
            if (mSelectedCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中客户？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {

                //mSelectedCommonItem.IsDeleted = 1;
                mSelectedCustomer.Status = "STATUS05";
                bool successful = MstCustomerBs.UpdateMstCustomer(mSelectedCustomer);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.Modify;
            frm.selectedCustomer = mSelectedCustomer;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedCustomer.Status != "STATUS01" && mSelectedCustomer.Status != "STATUS02")
            {
                MessageBox.Show("该客户当前状态不可审核！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.Approve;
            frm.selectedCustomer = mSelectedCustomer;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Submit()
        {
            if (mSelectedCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedCustomer.Status != "STATUS01")
            {
                MessageBox.Show("该客户当前状态不可送审！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否送审选中客户？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedCustomer.Status = "STATUS02";
                bool successful = MstCustomerBs.UpdateMstCustomer(mSelectedCustomer);
                if (successful)
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Export()
        {
            SaveFileDialog frm = new SaveFileDialog();
            frm.Filter = "All Files (*.*)|";
            frm.DefaultExt = "xls";
            frm.AddExtension = true;
            frm.FileName = this.Text + DateTime.Today.ToString("yyyy-MM-dd");
            if (frm.ShowDialog() == DialogResult.OK)
            {
                gvMain.ExportToXls(frm.FileName);
            }
        }

        private void btnApprovedModify_Click(object sender, EventArgs e)
        {
            if (mSelectedCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.ApprovedModify;
            frm.selectedCustomer = mSelectedCustomer;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnCustomerInfo_Click(object sender, EventArgs e)
        {
            if (mSelectedCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.View;
            frm.selectedCustomer = mSelectedCustomer;
            frm.ShowDialog();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            btnView.Enabled = false;
            btnApprovedModify.Enabled = false;
            btnApprove.Enabled = false;
            btnModify.Enabled = false;
            btnDelete.Enabled = false;

            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedCustomer = MstCustomerBs.GetMstCustomerByID(CommonFunctions.ToString(dr["CUSTOMER_ID"]));
            if (mSelectedCustomer.Status == "STATUS01")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedCustomer.Status == "STATUS02")
            {
                btnApprove.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if (mSelectedCustomer.Status == "STATUS03")
            {
                btnView.Enabled = true;
                btnDelete.Enabled = true;
                btnApprovedModify.Enabled = true;
            }
            else if (mSelectedCustomer.Status == "STATUS04")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
            }
            else if (mSelectedCustomer.Status == "STATUS05")
            {
            }
        }

        private void btnSalesFee_Click(object sender, EventArgs e)
        {
            frmMaintainSalesFeeExpenseOrder frm = new frmMaintainSalesFeeExpenseOrder(mSelectedCustomer);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnSalesFeeList_Click(object sender, EventArgs e)
        {
            frmManageSalesApprovedFee frm = new frmManageSalesApprovedFee();
            if (mSelectedCustomer != null)
            {
                frm = new frmManageSalesApprovedFee(mSelectedCustomer);
            }
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnShowAllCustomer_Click(object sender, EventArgs e)
        {
            //frmPersonCustomerOverview frm = new frmPersonCustomerOverview((List<MstPersonEntity>)cmbPerson.DataSource);
            //frm.ThisActionType = ActionType.View;
            //frm.ShowDialog();
        }
        #endregion

    }
}
